<template>
  <div class="goodsdetial">
    <myheader>
      <template #left>
        <span @click="$router.back()">返回</span></template
      ></myheader
    >
    <!-- 商品图片 -->
    <goodsbox>
      <template #top>
        <div>
          <!-- <img :src="pic" alt="" style="width: 100%" /> -->
          <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
            <van-swipe-item v-for="value in ImagesList" :key="value.img"
              ><img :src="value.src" alt="" />
            </van-swipe-item>
          </van-swipe></div
      ></template>
    </goodsbox>
    <!-- 商品标题 + 价格 -->
    <goodsbox>
      <template #top>
        <div class="title">{{ msg.title }}</div>
      </template>
      <template #mid>
        <div class="mid"></div>
      </template>
      <template #bottom>
        <div class="price">
          <span class="market"
            >市场价：<i>¥{{ msgList.market_price }}</i></span
          >
          <span class="sell"
            >销售价：<i>¥{{ msgList.sell_price }}</i></span
          >
        </div>
        <div class="buy">
          购买数量:<van-stepper v-model="value" integer @change="onChange" />
        </div>
        <div class="btn">
          <van-button type="info">立即购买</van-button>
          <van-button type="danger" @click="addcart">加入购物车</van-button>
        </div>
      </template>
    </goodsbox>
    <!-- 商品参数 + 信息 -->
    <goodsbox>
      <template #top>商品参数</template>
      <template #mid>
        <div class="mid"></div>
      </template>
      <template #bottom>
        <div class="arguments">
          <p>商品货号：{{ msgList.goods_no }}</p>
          <p>库存情况：{{ msgList.stock_quantity }}</p>
          <p>上架时间：{{ msgList.add_time }}</p>
        </div>
      </template>
    </goodsbox>
    <!-- 图文介绍 +商品评论 -->
    <goodsbox>
      <template #top>
        <div
          class="imgtext"
          @click="$router.push({ name: 'desc', params: { id: goodsId } })"
        >
          图文详情
        </div>
      </template>
      <template #bottom>
        <div
          class="comment"
          @click="
            $router.push({ name: 'GoodsComment', params: { id: goodsId } })
          "
        >
          商品评论
        </div>
      </template>
    </goodsbox>
  </div>
</template>

<script>
import myheader from "@/components/myheader";
import goodsbox from "@/components/goodsbox";
import {
  getGoodsDetial,
  getGoodInfo,
  getCarList,
  getImages,
} from "@/apis/goods";
export default {
  props: ["id"],
  components: {
    myheader,
    goodsbox,
  },
  data() {
    return {
      goodsId: "",
      msg: [],
      msgList: [],
      pic: "",
      // 购买数量
      value: 1,
      // 需要id，count，title，pic_path，price，
      // 购物车信息
      cartList: [],
      ImagesList: [],
    };
  },
  mounted() {
    getImages(this.id).then((res) => {
      console.log(res);
      this.ImagesList = res.data.message;
    });
    // console.log(this.$route.params.id);
    this.goodsId = this.$route.params.id;
    // console.log(this.goodsId);
    // console.log(this.id);
    getGoodsDetial(this.id).then((res) => {
      this.msg = res.data.message[0];
    });
    getGoodInfo(this.id).then((res) => {
      this.msgList = res.data.message[0];
    });
    getCarList(this.id).then((res) => {
      // 图片路径
      this.pic = res.data.message[0].thumb_path;
    });
  },

  methods: {
    // 获取商品变化的数量
    onChange(value) {
      this.value = value;
    },
    // 添加购物车
    addcart() {
      // 判断localStorage中是否存在这个id，
      // 如果存在数量则叠加，如果不存在，则在撞创建一个数组
      // 从本地取数据
      let carlist = JSON.parse(localStorage.getItem("carList")) || [];

      //过滤出商品id相同的单条数据
      let arr = carlist.filter((v) => {
        return v.id == this.id;
      });

      // 判断如果有数据则修改商品数量
      if (arr.length != 0) {
        arr[0].count += this.value;
        // 先用过滤删除掉本地数据中的自己页面的商品数据
        carlist = carlist.filter((v) => {
          return v.id != this.id;
        });
        // 重新添加本页商品数据
        carlist.push(arr[0]);
      } else {
        // 数组为空，本地里面没有相同商品的数据，就添加新数据，定义一个对象保存数据
        let obj = {
          id: this.id,
          count: this.value,
          title: this.msgList.title,
          pic_path: this.pic,
          price: this.msgList.sell_price,
        };
        carlist.push(obj);
      }
      localStorage.setItem("carList", JSON.stringify(carlist));
      // 输入框变为1
      this.value = 1;
    },
  },
};
</script>

<style lang="less" scoped>
.goodsdetial {
  padding-bottom: 60px;
  img {
    width: 100%;
  }
  .mid {
    width: 100%;
    height: 2px;
    margin-top: 20px;
    margin-bottom: 20px;
    background-color: #ccc;
  }
  .title {
    font-size: 20px;
    font-weight: 700;
    color: #26a2ff;
    padding-left: 10px;
  }
  .price,
  .buy {
    padding-left: 20px;
    color: #5c5c5c;
    font-weight: normal;
    font-size: 15px;
    padding-bottom: 15px;
    display: flex;
    .market {
      i {
        text-decoration: line-through;
      }
    }
    .sell {
      margin-left: 20px;
      i {
        color: red;
        font-size: 20px;
        font-weight: 700;
      }
    }
  }
  .btn {
    padding-left: 20px;
  }
  .van-button {
    border-radius: 5px;
    margin-right: 5px;
  }
  .arguments {
    padding-left: 40px;
  }
  .imgtext {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border: 2px solid #26a2ff;
    color: #26a2ff;
    border-radius: 5px;
    margin-bottom: 10px;
  }
  .comment {
    width: 100%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 5px;
    border: 2px solid #ef4f4f;
    color: #ef4f4f;
  }
}
// 价格
</style>
